package cz.salixsoft.bugreporter.adapter.mantis;

import cz.salixsoft.bugreporter.Bug;
import cz.salixsoft.bugreporter.BugConnetionManager;
import cz.salixsoft.bugreporter.Priority;
import cz.salixsoft.bugreporter.Severity;
import java.math.BigInteger;
import org.mantis.ta.MantisManager;
import org.mantis.ta.beans.IssueData;
import org.mantis.ta.beans.ObjectRef;

/**
 * @author Vrbenský Milan
 * @version 1.0 @created 01-II-2012 15:59:53
 */
public class MantisConnectorManagerImpl implements BugConnetionManager {

    /**
     * Id of project where the new Issues will be created.
     */
    private BigInteger projectId;

    /**
     * Category of issue. Corresponds to category in Mantis. Default value is
     * "General".
     */
    private String category = "General";

    /**
     * Manager of connection to Mantis.
     */
    private MantisManager manager;

    /**
     * Convertor of priority. Converts priority form bug reporter to priority
     * values of Mantis.
     */
    private MantisPriorityConverter mpc = new MantisPriorityConverter();

    /**
     * Severity of priority. Converts severity form bug reporter to severity
     * values of Mantis.
     */
    private MantisSeverityConverter msc = new MantisSeverityConverter();

    /**
     * Creates connection manager
     *
     * @param mantisManager Mantis manager
     * @param projectId     Identification of project in Mantis
     */
    public MantisConnectorManagerImpl(MantisManager mantisManager, BigInteger projectId) {
        this.projectId = projectId;
        this.manager = mantisManager;
    }

    /**
     * Send a new Issue to Mantis.
     *
     * @param bug Bug which will by send to Mantis
     */
    public void createBug(Bug bug) {

        // Create issue data
        IssueData data = createIssueData(bug);

        // Send the data to Mantis
        sendIssueToMantis(data);
    }

    /**
     * Creates issue from informations if bug. This method automaticly set up
     * the category and project of issue. If priority and severity is not set,
     * is sets the values to default.
     *
     * @param bug Bug which will by send to Mantis
     *
     * @return Issue data
     */
    public IssueData createIssueData(Bug bug) {

        IssueData issue = new IssueData();

        issue.setCategory(category);

        issue.setProject(getObjectRefForProjectId(projectId));
        issue.setPriority(getObjectRefForIssuePriorityId(mpc.getPriority(bug.getPriority())));
        issue.setSeverity(getObjectRefForIssueSeverityId(msc.getSeverity(bug.getSeverity())));
        issue.setSummary(bug.getSummary());
        issue.setDescription(bug.getDescription());
        
        return issue;
    }

    /**
     * Sends the issue to Mantis. It is not valid to send NULL as value of the
     * parameter.
     *
     * @param data Issue data.
     */
    protected void sendIssueToMantis(IssueData data) {
        try {
            manager.createIssue(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Creates Mantis object reference to project from projects id.
     *
     * @param projectId Identification of project
     *
     * @return Object reference to project
     */
    protected ObjectRef getObjectRefForProjectId(BigInteger projectId) {
        ObjectRef projectRef = new ObjectRef();
        projectRef.setId(projectId);

        return projectRef;
    }

    /**
     * Creates Mantis object reference to severity from severity id.
     *
     * @param severityId Identification of severity
     *
     * @return Object reference to severity
     */
    protected ObjectRef getObjectRefForIssueSeverityId(BigInteger severityId) {
        ObjectRef severityRef = new ObjectRef();
        severityRef.setId(severityId);

        return severityRef;
    }

    /**
     * Creates Mantis object reference to priority from priority id.
     *
     * @param priorityId Identification of priority
     *
     * @return Object reference to priority
     */
    protected ObjectRef getObjectRefForIssuePriorityId(BigInteger priorityId) {
        ObjectRef issueRef = new ObjectRef();
        issueRef.setId(priorityId);

        return issueRef;
    }

    /**
     * Id of project where the new Issues will be created.
     */
    protected BigInteger getProjectId() {
        return projectId;
    }

    /**
     * Id of project where the new Issues will be created.
     *
     * @param projectId
     */
    protected void setProjectId(BigInteger projectId) {
        this.projectId = projectId;
    }

    /**
     * @return MantisManager instance.
     */
    public MantisManager getManager() {
        return manager;
    }

    /**
     * Category of issue. Corresponds to category in Mantis. Default value is
     * General.
     */
    public String getCategory() {
        return category;
    }

    /**
     * Category of issue. Corresponds to category in Mantis. Default value is
     * General.
     *
     * @param category
     */
    public void setCategory(String category) {
        this.category = category;
    }

    /**
     * Sets the default priority of bug. It is used when the bug has priority
     * Priority.NOT_DEFINED, which is the default value of bug.
     *
     * @param priority
     */
    public void setDefaultPriority(Priority priority) {
        mpc.setDefaultPriority(priority);
    }

    /**
     * Sets the default severity of bug. It is used when the bug has severity
     * Severity.NOT_DEFINED, which is the default value of bug.
     *
     * @param severity
     */
    public void setDefaultSeverity(Severity severity) {
        msc.setDefaultSeverity(severity);
    }
}